package com.lovo.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lovo.sys.bean.ProductBean;
import com.lovo.sys.bean.PromotionalSchemesBean;
import com.lovo.sys.mapper.IProductMapper;
import com.lovo.sys.mapper.IPromoyionalSchemesMapper;
import com.lovo.sys.service.IPromotionalSchemesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 促销实现类
 */
@Transactional
@Service
public class PromotionalSchemesServiceImpl implements IPromotionalSchemesService {
    @Autowired
    private IProductMapper productMapper;

    @Autowired
    private IPromoyionalSchemesMapper promoyionalSchemesMapper;

    @Override
    public IPage<ProductBean> findProductAll(Integer pageNum) {
        QueryWrapper<ProductBean> qw = new QueryWrapper<>();
        qw.eq("p_status", 2);
        return productMapper.selectPage(new Page<>(pageNum, 5), qw);
    }

    @Override
    public List<PromotionalSchemesBean> findByPromotionalScheme() {
        QueryWrapper<PromotionalSchemesBean> qw = new QueryWrapper<>();
        qw.eq("p_status",0);
        return promoyionalSchemesMapper.selectList(qw);
    }

    @Override
    public void addPromotional(PromotionalSchemesBean promotionalSchemesBean) {
        promoyionalSchemesMapper.insert(promotionalSchemesBean);
    }

    @Override
    public void addProductByPromotional(float rate, Integer[] productId, long promotionalId) {
        promoyionalSchemesMapper.addProductByPromotional(rate, productId, promotionalId);
    }
    @Override
    public void delPromotional(long promotionalId) {
        QueryWrapper<PromotionalSchemesBean> qw = new QueryWrapper<>();
        qw.eq("pk_promotionalId",promotionalId);
        PromotionalSchemesBean promotionalSchemesBean = promoyionalSchemesMapper.selectOne(qw);
        promotionalSchemesBean.setStatus(1);
        promoyionalSchemesMapper.updateById(promotionalSchemesBean);
        promoyionalSchemesMapper.delByPromotionalByProduct(promotionalId);
    }

    @Override
    public void updateStatus(String number, int status) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("p_number",number);
        PromotionalSchemesBean promotionalSchemesBean = promoyionalSchemesMapper.selectOne(qw);
        promotionalSchemesBean.setAudit(status);
        promoyionalSchemesMapper.updateById(promotionalSchemesBean);
    }

    @Override
    public List<String> findByProductId(Integer[] productId) {
        return promoyionalSchemesMapper.findByProductId(productId);
    }
}
